package com.yidu.ruifeng_logistics.utils;


import com.yidu.ruifeng_logistics.entity.LogisticsArea;
import com.yidu.ruifeng_logistics.service.LogisticsAreaService;
import com.yidu.ruifeng_logistics.service.impl.LogisticsAreaServiceImpl;
import com.yidu.ruifeng_logistics.vo.LogisticsPredictionVo;

import java.util.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 肖楚科
 * @date:2020/11/17
 * @time:9:00
 * @blog:com.yidu.util
 * @description:常用工具类
 */
public class Tools {
    /**
     * 返回LAYUI需要的格式
     *
     * @param o 所需数据
     * @param count 行数
     * @return map
     */
    public static Map<String, Object> getLayUiMap(Object o, int count) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "");
        map.put("code", 0);
        map.put("data", o);
        map.put("count", count);
        return map;
    }

    /**
     * 解析字符串为日期
     * <p>
     * 说明：
     * 1. 该方法主要解决yyyy-MM-dd格式文本字符串无法直接转换为LocalDateTime的问题
     *
     * @param source        需要解析的日期字符串
     * @param formatPattern 日期格式化模式
     * @return 格式化后的日期
     */
    public static LocalDateTime parseStringToLocalDateTime(String source, String formatPattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern);
        LocalDate localDate = LocalDate.parse(source, dateTimeFormatter);
        return localDate.atStartOfDay();
    }

    public static LogisticsPredictionVo selectAreaName(List<LogisticsPredictionVo> logisticsPredictionVos){
       LogisticsAreaService logisticsAreaService = new LogisticsAreaServiceImpl();
        LogisticsPredictionVo predictionVo = new LogisticsPredictionVo();
        for (LogisticsPredictionVo vo :logisticsPredictionVos){
            List<LogisticsArea> area = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionBourn()));
            for(LogisticsArea area1:area){
                predictionVo.setPredictionBourn(area1.getAreaName());
            }
            List<LogisticsArea> areas = logisticsAreaService.selectYi(Integer.valueOf(vo.getPredictionDeparture()));
            for(LogisticsArea area1:areas){
                predictionVo.setPredictionDeparture(area1.getAreaName());
            }

        }
        return predictionVo;

    }

}
